package com.zero.java.base.stream;

import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo {

    public static void main(String[] args) {

        Stream<Locale> locales = Stream.of(Locale.getAvailableLocales());
        Map<String, Long> countryToLocaleCounts = locales.collect(Collectors.groupingBy(Locale::getCountry,Collectors.counting()));
        System.out.println(countryToLocaleCounts);
        Long chNum = countryToLocaleCounts.get("CH");
        System.out.println("CH count number:"+chNum);

        //toSet()
        locales = Stream.of(Locale.getAvailableLocales());
        Map<String, Set<Locale>> countryToLocaleToSet = locales.collect(Collectors.groupingBy(Locale::getCountry, Collectors.toSet()));
        System.out.println(countryToLocaleToSet);
        Set<Locale> chSet = countryToLocaleToSet.get("CH");
        for (Locale ch :chSet) {
            System.out.println(ch);
        }
        System.out.println(chSet);    //[de_CH, fr_CH, it_CH]

        locales = Stream.of(Locale.getAvailableLocales());
        Map<String, Set<String>> countryToLanguage = locales.collect(Collectors.groupingBy(Locale::getDisplayCountry, Collectors
                .mapping(Locale::getDisplayLanguage, Collectors.toSet())));
        System.out.println(countryToLanguage);
    }
}
